package com.zzccyy.jpdemo.page

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.animation.ValueAnimator.AnimatorUpdateListener
import android.content.Intent
import android.os.Build
import android.text.TextUtils
import android.view.View
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.SeekBar
import android.widget.TextView
import com.sztle.admodule.IPolicyDialogCallback
import com.sztle.admodule.apiprocessor.OutCommProcessor
import com.sztle.admodule.apiprocessor.listener.IStartProgress
import com.sztle.admodule.control.manager.ControlManager
import com.sztle.admodule.myutils.PolicyDialogHelper
import com.sztle.admodule.outstatus.AppStatusProcessor
import com.zzccyy.jpdemo.R
import com.zzccyy.jpdemo.base.BasicBaseActivity

class SplActivity : BasicBaseActivity() {
    private var adContainer: FrameLayout? = null
    private var progressLine: SeekBar? = null

    private var tvDevInfo: TextView? = null


    protected fun hideBottomUIMenu() {
        var flags: Int
        val curApiVersion = Build.VERSION.SDK_INT
        // This work only for android 4.4+
        flags = if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
            (View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_IMMERSIVE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
        } else {
            // touch the screen, the navigation bar will show
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        }
        val originFlag = window.decorView.systemUiVisibility
        flags = flags or originFlag
        window.decorView.systemUiVisibility = flags
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        hideBottomUIMenu()
    }

    override val layoutId: Int
        get() = R.layout.activity_spl

    override fun findViews() {
    }

    override fun setListener() {
    }

    override fun initData() {
        adContainer = findViewById<FrameLayout?>(R.id.ad_container)
        progressLine = findViewById(R.id.progress_line)
        tvDevInfo = findViewById(R.id.tv_dev_info)
        checkWaitGetConfig()
    }

    protected var animator: ValueAnimator? = null

    //todo list 启动页一进来开始等待获取配置
    private fun checkWaitGetConfig() {
        animator = ValueAnimator.ofInt(0, 100)
        animator!!.setDuration(5000)
        animator!!.setInterpolator(LinearInterpolator())
        animator!!.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                if (ControlManager.canStartInitSdk()) {
                    //获取配置，判断可以启动三方sdk初始化
                    startInitSdkAndLoading()
                    return
                }
                //获取配置后，表明需要先弹隐私协议框
                showPrivacyDialog()
            }
        })

        animator!!.addUpdateListener(AnimatorUpdateListener { animation ->
            //不断检测是否已经获取配置
            progressLine!!.progress = (animation.animatedValue as Int)!!
            //不断检测是否获取到了服务配置
            if (AppStatusProcessor.isGetAppServerConfig()) {
                //获取到配置后，则停止进度条，进行下一步判断是否弹隐私协议框
                if (!animator!!.isPaused()) {
                    animator!!.pause()
                    animator!!.end()
                }
            }
        })
        animator!!.start()
    }

    //todo list 启动三方sdk初始化,启动进度条时间设置为10s
    private fun startInitSdkAndLoading() {
        //todo list 启动三方sdk初始化
        AppStatusProcessor.statusInitSdk(this)
        animator = ValueAnimator.ofInt(10, 100)
        animator!!.setDuration(10000)
        animator!!.setInterpolator(DecelerateInterpolator())
        animator!!.addUpdateListener(AnimatorUpdateListener { animation ->
            if (!TextUtils.isEmpty(OutCommProcessor.getSsid())) {
                //获取到ssid后，显示ssid（哈希后的oaid）
                tvDevInfo!!.text = OutCommProcessor.getSsid()
            }
            val value = animation.animatedValue as Int
            progressLine!!.progress = value
            //todo list 启动过程中需不断去做一些风控检测,IStartProgress为风控过程中控制启动页面结束或者暂停或者恢复继续 传递实现方法。
            //adContainer 为开屏广告使用的容器。
            AppStatusProcessor.statusLoadingProgressCheck(
                this@SplActivity,
                adContainer,
                object : IStartProgress() {
                    override fun cancelLoadingProgress() {
                        //todo list 启动进度条取消，则结束启动页面
                        if (animator != null) {
                            animator!!.cancel()
                        }
                    }

                    override fun pauseLoadingProgress() {
                        //todo list 暂停启动页面
                        if (animator != null) {
                            animator!!.pause()
                        }
                    }

                    override fun resumeLoadingProgress() {
                        //todo list 恢复启动页面
                        if (animator != null) {
                            animator!!.resume()
                        }
                    }
                })
        })

        animator!!.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                progressLine!!.progress = 100
                gotoNextPage()
            }

            override fun onAnimationCancel(animation: Animator) {
                super.onAnimationCancel(animation)
                gotoNextPage()
            }
        })
        animator!!.start()
    }

    //todo list 弹隐私协议框
    private fun showPrivacyDialog() {
        PolicyDialogHelper.showPolicyDialog(this, object : IPolicyDialogCallback() {
            override fun onSure() {
                //同意后，开始加载三方sdk
                startInitSdkAndLoading()
            }

            override fun onCancel() {
                finish()
            }

            override fun onClickUserPolicy() {
                //todo list 跳转用户协议页面，开发人员可自己定义页面及跳转方式
                val bundle = Intent(this@SplActivity, WebActivity::class.java)
                bundle.putExtra("type", 0)
                startActivity(bundle)
            }

            override fun onClickPrivacyPolicy() {
                //todo list 跳转隐私协议页面，开发人员可自己定义页面及跳转方式
                val bundle = Intent(this@SplActivity, WebActivity::class.java)
                bundle.putExtra("type", 1)
                startActivity(bundle)
            }
        })
    }

    private fun gotoNextPage() {
        if (animator != null) {
            //todo list 移除一切监听，防止多次跳转到主页面。
            animator!!.removeAllListeners()
        }
        val bundle = Intent(this@SplActivity, HomeActivity::class.java)
        startActivity(bundle)
        finish()
    }

    override fun isNeedAd(): Boolean {
        return false
    }

}